package com.yaorange.nongjt.service.impl;

import com.yaorange.common.core.domain.entity.SysUser;
import com.yaorange.common.core.domain.model.LoginUser;
import com.yaorange.common.exception.ServiceException;
import com.yaorange.common.utils.SecurityUtils;
import com.yaorange.nongjt.domain.NjtLoginUser;
import com.yaorange.nongjt.domain.NjtUserPerfect;
import com.yaorange.nongjt.domain.NjtUserRegister;
import com.yaorange.nongjt.service.INjtCommonService;
import com.yaorange.nongjt.service.INjtUserPerfectService;
import com.yaorange.nongjt.service.INjtUserRegisterService;
import com.yaorange.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NjtCommonServiceImpl implements INjtCommonService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private INjtUserPerfectService userPerfectService;
    @Autowired
    private INjtUserRegisterService userRegisterService;
    @Override
    public NjtLoginUser getLoginUser() {
        // 当前登录系统⽤户
        LoginUser sysUser = SecurityUtils.getLoginUser();
        if (sysUser == null) {
            throw new ServiceException("当前登录信息查询失败");
        }
        // 查询最新的系统⽤户信息，避免前端更新⽤户信息后，未重新登录导致数据未刷新情况
        SysUser user = sysUserService.selectUserById(sysUser.getUserId());
        if (null != user) {
            sysUser.setUser(user);
        }
        NjtLoginUser loginUser = new NjtLoginUser();
        loginUser.setSysUser(sysUser);
        // 查询会员注册信息
        NjtUserRegister userRegister = userRegisterService.getByMobilePhone(sysUser.getUsername());
        if (null != userRegister) {
            loginUser.setUserRegister(userRegister);
            Long userId = userRegister.getId();
            // 查询会员完善信息
            NjtUserPerfect userPerfect = userPerfectService.getByRegisterUserId(userId);
            if (null != userPerfect) {
                loginUser.setUserPerfect(userPerfect);
            }
        }
        return loginUser;
    }
}